﻿#include<iostream>
using namespace std;
int main() {
	//int a[10]{ 105,98,73,58,32,31,25,22,3,1 };//倒序
	int a[10]{ 1,3,22,25,31,32,58,73,98,105 };//正序

	int acount = sizeof(a) / sizeof(int);//数组长度
	int* anew = new int[sizeof(a) + 1];//新动态数组

	int x;
	cout << "输入一个数字：";
	cin >> x;

	int getIndex{ acount };//前面重复能与后一位比较，后面重复没得比较，所以设定为acount
	bool bcase = a[0] > a[1];

	//第一种比较方式
	for (int i = 0; i < acount; i++) {
		if (a[0] > a[1]) {
			if (x > a[i]) {
				getIndex = i;
				break;
			}
		}

		else {
			if (x < a[i]) {
				getIndex = i;
				break;
			}
		}
	}

	//第二种比较方式
	for (int i = 0; i < acount; i++) {
		if (bcase ^ (x < a[i])) {//这里能判断是因为无论正序还是倒序，x < a[i]的值都能随着改变，bcase的a[0] > a[1]也是
			getIndex = i;//当是正序时，x < a[i]的值为0，bcase的a[0] > a[1]为0，异或得0
			break;//当是倒序时，x < a[i]的值为1，bcase的a[0] > a[1]为1，异或得0
		}
	}

	::memcpy(anew, a, sizeof(int) * getIndex);
	::memcpy(anew + getIndex + 1, a + getIndex, (acount - getIndex) * sizeof(int));

	anew[getIndex] = x;

	for (int i = 0; i <= acount; i++) {
		cout << anew[i] << " ";
	}
}